//
//  MapOperationDemoViewController.m
//  FMDemoBaseMap
//
//  Created by fengmap on 16/10/17.
//  Copyright © 2016年 fengmap. All rights reserved.
//
//  地图操作
//  简介：动画设置地图中心点，地图显示级别，地图旋转、倾斜角度；

#import "MapOperationDemoViewController.h"
#import "FMMapKit.h"
#import "FMKValueAnimation.h"

@interface MapOperationDemoViewController () <FMKMapViewDelegate, FMKAnimationDelegate>
{
    FMKMapView *_mapView;
    // 地图上的某个点，将此点移至屏幕中心
    FMKGeoCoord _coord;
    // 移动动画中，用于存储移动地图起始点的临时变量
    CGPoint _moveAnimationTemp;
    
    /// 需要设置的地图显示级别
    int _zoomLevel;
    /// 需要设置的地图倾斜角度
    float _inclineAngle;
    /// 需要设置的地图旋转角度
    float _rotateAngle;
}
@end


@implementation MapOperationDemoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 离线地图加载
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"jlai-cyds-museum02" ofType:@"fmap"];
    _mapView = [[FMKMapView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) path:dataPath delegate:self];
    [self.view addSubview:_mapView];
    
    NSString *themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"jlai-cyds-museum02/jlai-cyds-museum02.theme"];
    [_mapView setThemeWithLocalPath:themePath];
    
    //_coord = FMKGeoCoordMake(1, FMKMapPointMake(12961640, 4861800));
    _coord = FMKGeoCoordMake(1, FMKMapPointMake(0, 0));
    _zoomLevel = 20;
    _inclineAngle = 45;
    _rotateAngle = 60;
    
    [self createButton];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createButton
{
    NSArray *btnNames = @[
                          [NSString stringWithFormat:@"地图中心点移动至：楼层(groupID)：%d\n坐标(mapPoint)：(%.2f, %.2f)", _coord.groupID, _coord.mapPoint.x, _coord.mapPoint.y],
                          [NSString stringWithFormat:@"地图显示级别改为：%d", _zoomLevel],
                          [NSString stringWithFormat:@"地图倾斜角度改为：%.2f", _inclineAngle],
                          [NSString stringWithFormat:@"地图旋转角度改为：%.2f", _rotateAngle]
                          ];
    for (int i = 0; i < 4; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(10, SCREEN_HEIGHT-(4-i)*50, SCREEN_WIDTH-10*2, 44);
        [btn setTitle:btnNames[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.numberOfLines = 0;
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        btn.backgroundColor = [UIColor whiteColor];
        btn.layer.shadowColor = [UIColor grayColor].CGColor;
        btn.layer.shadowOpacity = 0.6;
        btn.layer.shadowOffset = CGSizeMake(1, 1);
        btn.tag = i;
        btn.selected = YES;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
}

#pragma mark - FMKMapViewDelegate
- (void)mapViewDidFinishLoadingMap:(FMKMapView *)mapView {
    NSLog(@"地图加载完成");
}
- (void)mapViewDidFailLoadingMap:(FMKMapView *)mapView withError:(NSError *)error {
    NSLog(@"地图加载失败-%@", error);
}

#pragma mark - 按钮点击
- (void)btnClick:(UIButton *)sender
{
    switch (sender.tag) {
        case 0: {
            // 将某个地图坐标点移动到屏幕中央(SDK方法)
            [_mapView moveToViewCenterFromMapPoint:_coord.mapPoint animated:YES];
            // 移动地图(自己实现动画)
//            CGPoint point = [_mapView coverCoord:_coord zType:FMKMAPCOORDZ_EXTENT];
//            [self moveAnimation:point end:_mapView.center];
        }
            break;
        case 1:
            // 设置缩放级别
            [_mapView.map setZoomLevel:_zoomLevel animated:YES];
            break;
        case 2:
            // 设置地图倾斜角度（动画）
            [self setMapInclineAngle:_inclineAngle];
            break;
        case 3: {
            // 设置地图旋转角度（动画）
            // 让地图以最小角度旋转
            _mapView.map.mapStatus.rotateAngle = (int)(_mapView.map.mapStatus.rotateAngle)%360 + _mapView.map.mapStatus.rotateAngle - (int)(_mapView.map.mapStatus.rotateAngle);
            if ((_mapView.map.mapStatus.rotateAngle - _rotateAngle) > 180) {
                _mapView.map.mapStatus.rotateAngle = _mapView.map.mapStatus.rotateAngle-360;
            }
            else if ((_mapView.map.mapStatus.rotateAngle - _rotateAngle) < -180) {
                _mapView.map.mapStatus.rotateAngle = _mapView.map.mapStatus.rotateAngle+360;
            }
            [self setMapRotateAngle:_rotateAngle];
        }
            break;
    }
}

#pragma mark - 移动地图（动画）
/**
 *  地图移动动画
 *
 *  @param start 起点坐标 屏幕坐标
 *  @param end   终点坐标 屏幕坐标
 */
- (void)moveAnimation:(CGPoint)start end:(CGPoint)end
{
    _moveAnimationTemp = start;
    FMKValueAnimation * animation = [[FMKValueAnimation alloc] initWithDuration:1.f key:@"moveAnimation"];
    FMKCubicInterpolator *interpolator = [[FMKCubicInterpolator alloc] initWithStageType:FMKSTAGE_OUT];
    [animation setInterpolator:interpolator];
    animation.delegate = self;
    animation.fromValue = [NSValue valueWithCGPoint:start];
    animation.toValue = [NSValue valueWithCGPoint:end];
    [animation start];
}

#pragma mark - 设置地图倾斜角度（动画）
- (void)setMapInclineAngle:(float)angle
{
    FMKValueAnimation * animation = [[FMKValueAnimation alloc] initWithDuration:1.f key:@"inclineAnimation"];
    FMKCubicInterpolator *interpolator = [[FMKCubicInterpolator alloc] initWithStageType:FMKSTAGE_OUT];
    [animation setInterpolator:interpolator];
    animation.delegate = self;
    animation.fromValue = @(_mapView.map.mapStatus.inclineAngle);
    animation.toValue = @(angle);
    [animation start];
}

#pragma mark - 设置地图旋转角度（动画）
- (void)setMapRotateAngle:(float)angle
{
    FMKValueAnimation * animation = [[FMKValueAnimation alloc] initWithDuration:1.f key:@"rotateAnimation"];
    FMKCubicInterpolator *interpolator = [[FMKCubicInterpolator alloc] initWithStageType:FMKSTAGE_OUT];
    [animation setInterpolator:interpolator];
    animation.delegate = self;
    animation.fromValue = @(_mapView.map.mapStatus.rotateAngle);
    animation.toValue = @(angle);
    [animation start];
}

#pragma mark - FMKAnimationDelegate
/**
 *  动画插值回调
 *
 *  @param value 插值结果
 *  @param key   key
 */
- (void)updateInterpolationValue:(id)value key:(NSString *)key
{
    if ([key isEqualToString:@"moveAnimation"]) {
        CGPoint tmpPoint = [(NSValue *)value CGPointValue];
        [_mapView moveFromStart:_moveAnimationTemp toEnd:tmpPoint];
        _moveAnimationTemp = tmpPoint;
    }
    else if ([key isEqualToString:@"inclineAnimation"]) {
        float tmpInclineAngle = ((NSNumber *)value).floatValue;
        _mapView.map.mapStatus.inclineAngle = tmpInclineAngle;
    }
    else if ([key isEqualToString:@"rotateAnimation"]) {
        float tmpRotateAngle = ((NSNumber *)value).floatValue;
        _mapView.map.mapStatus.rotateAngle = tmpRotateAngle;
    }
}

@end
